//
//  CCKPublishView.m
//  budejieMe
//
//  Created by 韦恺 on 16/8/25.
//  Copyright © 2016年 SuperJJ. All rights reserved.
//
#import "CCKPublishView.h"
#import "CCKPublishCell.h"
#import "CCKPublishModel.h"

@interface CCKPublishView () <UICollectionViewDelegate, UICollectionViewDataSource>


@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *publishFlowLayout;

@property (assign, nonatomic) CGSize itemSize;
@property (copy, nonatomic) NSArray *itemArr;

@end


@implementation CCKPublishView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor whiteColor];
    
    _publishCollectionView.delegate = self;
    _publishCollectionView.dataSource = self;
    
    UINib *nib = [UINib nibWithNibName:@"CCKPublishCell" bundle:nil];
    CCKPublishCell *cell = [nib instantiateWithOwner:nil options:nil].lastObject;
    
    [self.publishCollectionView registerNib:nib forCellWithReuseIdentifier:PUBLISH_CELL];
    
    CGSize itemSize = cell.frame.size;
    
    _itemSize = itemSize;
    
    _publishFlowLayout.itemSize = itemSize;
 
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [[NSBundle mainBundle] loadNibNamed:@"CCKPublishView" owner:nil options:nil].lastObject;
    
    [CCKPublishModel downloadPublishDataWithUrl:@"http://d.api.budejie.com/op/square/bs0315-iphone-4.3/0-100.json" successBlock:^(NSArray *dataArr) {
        
        _itemArr = dataArr;
        [_publishCollectionView reloadData];
        
    } failBlock:^(NSError *error) {
        
        NSLog(@"PublishView    %@", error);
        
    }];

    
    return self;
}

- (void)layoutSubviews
{
    [self layoutItems];
}

- (void)layoutItems
{
    CGFloat margin = 8;
    
    CGFloat itemSpacing = (kScreenWidth - 2 * margin - 5 * _itemSize.width) / 4;
    CGFloat lineSpacing = margin;
    
//    CGFloat collectionViewX = 0;
//    CGFloat collectionViewY = 0;
//    CGFloat collectionViewWidth = kScreenWidth;
//    CGFloat collectionViewHeight = _itemSize.height * 3;
    
//    _publishCollectionView.frame = CGRectMake(collectionViewX, collectionViewY, collectionViewWidth, collectionViewHeight);
    
//    self.frame = _publishCollectionView.frame;
    
    _publishFlowLayout.minimumLineSpacing = lineSpacing;
    _publishFlowLayout.minimumInteritemSpacing = itemSpacing;
    
    _publishFlowLayout.sectionInset = UIEdgeInsetsMake(margin * 2, margin, margin * 2, margin);
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _itemArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CCKPublishCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PUBLISH_CELL forIndexPath:indexPath];
    
    cell.publishModel = _itemArr[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(publishViewGetClickedWith:andIndexPath:)]) {
        
        [self.delegate publishViewGetClickedWith:self andIndexPath:indexPath];
    }
    
    
}



@end

